import logging, logging.handlers

def init_logging():
    # 创建日志器
    logger = logging.getLogger()
    # 设置日志级别
    logger.setLevel(logging.DEBUG)
    # 创建处理器
    fh = logging.handlers.TimedRotatingFileHandler(
        "D:\pycharm\webtp\log/tp.log",when="midnight",
         interval=1, backupCount=7, encoding="utf-8")
    sf = logging.StreamHandler()

    fh.setLevel(logging.INFO)
    sf.setLevel(logging.INFO)
    # 创建格式器
    fmt = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)]  %(message)s'
    formatter = logging.Formatter(fmt=fmt)
    # 在处理器添加格式
    fh.setFormatter(formatter)
    sf.setFormatter(formatter)
    # 在日志器添加处理器
    logger.addHandler(fh)
    logger.addHandler(sf)